﻿using System;
using System.Collections.Generic;
using PreeRegisterModel;
using Newtonsoft.Json.Linq;
using System.Data;

namespace PreeRegisterBLL
{
    public class ReportBLL
    {
        #region 长治和平接口 检验报告明细查询
        /// <summary>
        /// 获取检验报告列表 根据住院号+住院次数
        /// </summary>
        /// <param name="patientCode">住院号</param>
        /// <param name="times">住院次数【可空】</param>
        /// <returns></returns>
        public static List<ReportListResponseModel> GetReportList(string patientCode, string times)
        {
            //查询检验报告列表
            List<ReportListResponseModel> list = new List<ReportListResponseModel>();
            try
            {
                JObject obj_reportlist = ChangZhiHePingBLL.GetTestReportMainId("1", patientCode);//获取检验报告列表
                string code_reportlist = obj_reportlist["code"].ToString();
                if (string.Equals(code_reportlist, "1"))
                {
                    var mJObj_reportList = JArray.Parse(obj_reportlist["result"].ToString());//检验报告条目
                    foreach (var dr_reportList in mJObj_reportList)
                    {
                        ReportListResponseModel reportListRequestModel = new ReportListResponseModel();
                        reportListRequestModel.deptid = dr_reportList["ksbm"].ToString();//住院科室ID
                        reportListRequestModel.deptname = dr_reportList["ksmc"].ToString();//住院科室名称
                        reportListRequestModel.docname = dr_reportList["kdys"].ToString();//医生姓名
                        reportListRequestModel.hospid = CommonBLL.GetConfig("hospitalid");//医院ID
                        reportListRequestModel.hospname = CommonBLL.GetConfig("hospitalname");//医院名称
                        reportListRequestModel.otime = "";
                        reportListRequestModel.pid = CommonBLL.GetPid(patientCode, times);//患者唯一索引
                        reportListRequestModel.repid = dr_reportList["MainID"].ToString();
                        reportListRequestModel.title = dr_reportList["ReportTypeName"].ToString();
                        reportListRequestModel.time = dr_reportList["Cbsj"].ToString();//确认
                        reportListRequestModel.repnum = "";
                        reportListRequestModel.type = dr_reportList["yblx"].ToString(); ;//标本类型
                        reportListRequestModel.typeid = "";//标本号

                        hars_reportlist bll_reportlist = new hars_reportlist();
                        PreeRegisterModel.hars_reportlist model_reportlist = new PreeRegisterModel.hars_reportlist();
                        model_reportlist.createdate = model_reportlist.lastdate = DateTime.Now;
                        model_reportlist.deptid = reportListRequestModel.deptid;
                        model_reportlist.deptname = reportListRequestModel.deptname;
                        model_reportlist.docname = reportListRequestModel.docname;
                        model_reportlist.hospid = reportListRequestModel.hospid;
                        model_reportlist.hospname = reportListRequestModel.hospname;
                        model_reportlist.isdelete = 0;
                        model_reportlist.otime = reportListRequestModel.otime;
                        model_reportlist.pid = reportListRequestModel.pid;
                        model_reportlist.repid = reportListRequestModel.repid;
                        model_reportlist.repnum = reportListRequestModel.repnum;
                        model_reportlist.synmsg = "未同步";
                        model_reportlist.synstae = 0;
                        model_reportlist.time = reportListRequestModel.time;
                        model_reportlist.title = reportListRequestModel.title;
                        model_reportlist.type = reportListRequestModel.type;
                        model_reportlist.typeid = reportListRequestModel.typeid;
                        DataSet ds = bll_reportlist.GetList(model_reportlist.pid, model_reportlist.repid);
                        if (ds != null && ds.Tables[0].Rows.Count > 0)
                        {
                            string synsate = ds.Tables[0].Rows[0]["synstae"].ToString();
                            if (string.Equals(synsate, "0"))
                            {
                                list.Add(reportListRequestModel);
                            }
                        }
                        else
                        {
                            if (bll_reportlist.Add(model_reportlist))
                            {
                                list.Add(reportListRequestModel);
                            }
                            else
                            {
                                CommonBLL.WriteLog(string.Format("检验报告入库失败，患者ID：{0},医院侧检验报告条目ID：{1}"
                                     , model_reportlist.pid, model_reportlist.repid));
                            }
                        }
                    }
                }
            }
            catch (Exception err)
            {
                CommonBLL.WriteLog(string.Format("查询检验报告列表失败，住院号：{0},异常信息：{1}"
                                  , patientCode, err.ToString()));

            }
            return list;
        }
        public static ReportDetailResponseModel GetReportDetail(ReportDetailRequestModel reportDetailRequestModel)
        {
            //检验报告明细查询
            ReportDetailResponseModel reportDetailResponseModel = new ReportDetailResponseModel();

            //item 
            List<ReportDetailResponseItemModel> reportDetailResponseModel_list = new List<ReportDetailResponseItemModel>();

            try
            {
                JObject obj_reportdetail = ChangZhiHePingBLL.GetTestReportContent(reportDetailRequestModel.repid);//获取检验报告明细
                string code_reportdetail = obj_reportdetail["code"].ToString();
                if (string.Equals(code_reportdetail, "1"))
                {
                    var mJObj_reportdetail = JArray.Parse(obj_reportdetail["result"].ToString());
                    if (mJObj_reportdetail.ToString().Length > 3)
                    {
                        reportDetailResponseModel.code = "1";//code
                        reportDetailResponseModel.msg = "获取检验报告明细成功。";//msg
                        reportDetailResponseModel.deptname = "";
                        reportDetailResponseModel.docname = "";
                        reportDetailResponseModel.hospid = CommonBLL.GetConfig("hospitalid");//医院ID
                        reportDetailResponseModel.hospname = CommonBLL.GetConfig("hospitalname");
                        reportDetailResponseModel.repid = reportDetailRequestModel.repid;
                        reportDetailResponseModel.repnum = "";
                        reportDetailResponseModel.time = "";
                        ReportDetailResponseItemModel reportDetailResponseItemModel = new ReportDetailResponseItemModel();
                        ReportDetailResponseTitleModel reportDetailResponseTitleModel = new ReportDetailResponseTitleModel();
                        reportDetailResponseTitleModel.code = "代号";
                        reportDetailResponseTitleModel.dvalue = "比较值";
                        reportDetailResponseTitleModel.name = "项目名称";
                        reportDetailResponseTitleModel.result = "结果";
                        reportDetailResponseTitleModel.rvalue = "参考值";
                        reportDetailResponseTitleModel.unit = "单位";
                        reportDetailResponseTitleModel.ext1 = "扩展1";
                        reportDetailResponseTitleModel.ext2 = "扩展2";
                        reportDetailResponseItemModel.title = reportDetailResponseTitleModel;
                        List<ReportDetailResponseContentModel> reportDetailResponseContentModel_list = new List<ReportDetailResponseContentModel>();

                        foreach (var dr_reportdetail in mJObj_reportdetail)
                        {
                            ReportDetailResponseContentModel reportDetailResponseContentModel = new ReportDetailResponseContentModel();
                            reportDetailResponseContentModel.code = dr_reportdetail["TestItemNum"] == null ? "" : dr_reportdetail["TestItemNum"].ToString().Trim();
                            reportDetailResponseContentModel.dvalue = dr_reportdetail["TestResultSign"] == null ? "" : dr_reportdetail["TestResultSign"].ToString().Trim();
                            reportDetailResponseContentModel.name = dr_reportdetail["TestItemChi"] == null ? "" : dr_reportdetail["TestItemChi"].ToString().Trim();
                            reportDetailResponseContentModel.result = dr_reportdetail["TestResult"] == null ? "" : dr_reportdetail["TestResult"].ToString().Trim();
                            reportDetailResponseContentModel.rvalue = dr_reportdetail["ConsultChar"] == null ? "" : dr_reportdetail["ConsultChar"].ToString().Trim();
                            reportDetailResponseContentModel.unit = dr_reportdetail["Unit"] == null ? "" : dr_reportdetail["Unit"].ToString().Trim();
                            reportDetailResponseContentModel.ext1 = "";
                            reportDetailResponseContentModel.ext2 = "";
                            reportDetailResponseContentModel_list.Add(reportDetailResponseContentModel);
                        }
                        reportDetailResponseItemModel.content = reportDetailResponseContentModel_list;
                        reportDetailResponseModel_list.Add(reportDetailResponseItemModel);
                        reportDetailResponseModel.item = reportDetailResponseModel_list;
                    }
                    else
                    {
                        reportDetailResponseModel.code = "1";//code
                        reportDetailResponseModel.msg = string.Format("获取检验报告成功,但数据异常：{0}，报告单号ID：{1}"
                            , mJObj_reportdetail.ToString(), reportDetailRequestModel.repid);//msg
                    }
                }

                else
                {
                    reportDetailResponseModel.code = "0";//code
                    reportDetailResponseModel.msg = string.Format("获取检验报告明细失败,失败原因：{0}，报告单号ID：{1}"
                        , obj_reportdetail["msg"].ToString(), reportDetailRequestModel.repid);//msg
                }
            }
            catch (Exception err)
            {
                reportDetailResponseModel.code = "0";//code
                reportDetailResponseModel.msg = string.Format("获取检验报告明细异常,失败原因：{0}，报告单号ID：{1}"
                    , err.ToString(), reportDetailRequestModel.repid);//msg
            }
            //返回
            return reportDetailResponseModel;
        }

        #endregion
    }
}
